'use strict';

angular.module('app')
	.controller('clickCountController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '点击统计';
        $scope.param = { };
        $scope.loading = false;
        var myChart ;
        console.log( (new Date()).valueOf());
		var option="";
		$scope.search = function () {
		    console.log( (new Date()).valueOf());
            myChart = echarts.init(document.getElementById('main'));

            $scope.param.type= '1';
            $scope.param.source= '1';
            $scope.param.startTime=$("#startTime").val();
            $scope.param.endTime=$("#endTime").val();
	        $scope.loading = false;
			$.ajax({
				url : $scope.app.project_name + '/community/clickCount/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
                    myChart.clear();
                    myChart.showLoading();
                    $scope.setData(result.data);
				} else {
					$scope.msg = result.msg;
				}

				$scope.$apply();
			});
		}
		$scope.setData=function(data){
            var timeArr=[];
            var titleArr=['PV','UV'];
            var seriesArr=new Array();
            var countList=data;
            for(var j=0;j<countList.length;j++){
                timeArr[j]=countList[j].countTime;
            }
            var label='{"normal":{"show":"true","position":"top"}}';
            for (var i=0;i<titleArr.length;i++){
                var dataArr=new Array();
                for(var j=0;j<countList.length;j++){
                    if(titleArr[i] == 'PV'){
                        dataArr[j] = countList[j].pv;
                    }
                    if(titleArr[i] == 'UV'){
                        dataArr[j] = countList[j].uv;
                    }
                }
                var seriesStr='{"name":"'+titleArr[i]+'","type":"line","data":['+dataArr+'],"label":'+label+'}';
                seriesArr[i]=JSON.parse(seriesStr);
            }
            option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data:titleArr
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {

                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: timeArr
                },
                yAxis: {
                    type: 'value'
                },
                series:seriesArr
            };

            myChart.setOption(option);
             myChart.hideLoading();
            }
        $scope.search();

		$scope.searchRe=function(){
            $scope.param.startTime=$("#startTime").val();
            $scope.param.endTime=$("#endTime").val();
            if($scope.param.endTime!="" && $scope.param.startTime==""){
                $scope.alertModal({'content': '请选择开始时间'});
                return;
            }

            $.ajax({
                url : $scope.app.project_name + '/community/clickCount/list',
                data: $scope.param
            }).then(function(result) {
                $scope.loading = false;
                console.log(result.httpCode);
                if (result.httpCode == 200) {
                    myChart.clear();
                    myChart.showLoading();
                    $scope.setData(result.data);
                } else {
                    $scope.msg = result.msg;
                    myChart.hideLoading();
                }

                $scope.$apply();
            });
        }
        $scope.clearSearch = function() {
            $("#startTime").val("");
            $("#endTime").val("");
            $scope.search();
        }
} ]);